//
//  KKStatusPictureView.swift
//  
//
//  Created by kassia on 16/7/16.
//
//

import UIKit
//import SDWebImage
//重用标记
private let pictureCollectionIdentifier = "cell"
//配图间距
private let itemMargin: CGFloat = 5
//图片宽度
private let itemWidth = (screenWidth - itemMargin * 2 - originalMagrinCell * 2) / 3

//配图信息
class KKStatusPictureView: UICollectionView {

    var picUrls: [KKStatusPictureInfo]? {
    
        didSet {
        
            //配图的大小
            let size = countPictureSize(picUrls?.count ?? 0)
            
            //指定配图的大小
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(size)
            }
            messageLable.text = "\(picUrls?.count ?? 0)" // 显示图片张数
            
            
        }
    }
    
    //懒加载张数
    private lazy var messageLable: UILabel = {
    
      let lable = UILabel(textColor: UIColor.whiteColor(), fontSize: 15)
        
      return lable
    
    }()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        dataSource = self //数据源设置
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置UI
    private func setupUI() {
        registerClass(KKStatuaPictureCell.self, forCellWithReuseIdentifier: pictureCollectionIdentifier)
    
        addSubview(messageLable)
        
        messageLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)

        }
    }
    
    /**
     根据图片张数计算图片大小
     */
    private func countPictureSize(count: Int) -> CGSize {
    
        let col = count > 3 ? 3 : count // 列数
        let rol = (count - 1) / 3 + 1 // 行数
        
        let width = CGFloat(col) * itemWidth + CGFloat(col - 1) * itemMargin // 宽度
        let height = CGFloat(rol) * itemWidth + CGFloat(rol - 1) * itemMargin // 高度
        
        let size = CGSize(width: width, height: height)
        
        return size
    }
}

// MARK: - 数据源方法
extension KKStatusPictureView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCollectionIdentifier, forIndexPath: indexPath) as! KKStatuaPictureCell
  
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }

}

// 自定义cell
class KKStatuaPictureCell: UICollectionViewCell {
    
    //设置模型
    var picInfo: KKStatusPictureInfo? {
        
        didSet {
            
            if let imageUrl = picInfo?.thumbnail_pic {
                //设置图片
                imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                gifImage.hidden = !imageUrl.hasSuffix(".gif")
            }
            
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        //懒加载控件
    //配图的图片
    private lazy var imageView: UIImageView = {
    
       let image = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        //  保证原始图片比例图片
        image.contentMode = UIViewContentMode.ScaleAspectFill
        
        //  剪出超出多余
        image.clipsToBounds = true
        
        return image
    
    }()
    
    
    // GIF的图片
    private lazy var gifImage: UIImageView = {
    
        let gif = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        return gif
    
    }()
    
    //设置控件约束
    private func setUpUI() {
    
        addSubview(imageView)
        addSubview(gifImage)
        
        imageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImage.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    
    }

}


